package com.zzw.controller;

import com.zzw.bean.Msg;
import com.zzw.bean.Permissions;
import com.zzw.service.PermissionsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 权限管理控制器
 * 用于提供权限相关的API
 * @author 赵志伟
 * @version 1.0
 */
@Controller
@RequestMapping("/permissions")
@Api(tags = "权限管理接口")
public class PermissionsController {

    @Autowired
    private PermissionsService permissionsService;

    /**
     * 获取所有权限列表
     * @return 权限列表
     */
    @ApiOperation(value = "获取权限列表", notes = "获取所有权限列表")
    @GetMapping("/list")
    @ResponseBody
    public Msg list() {
        List<Permissions> permissions = permissionsService.selectAll();
        return Msg.success().add("permissions", permissions);
    }
    
    /**
     * 根据角色ID获取权限列表
     * @param roleId 角色ID
     * @return 权限列表
     */
    @ApiOperation(value = "根据角色ID获取权限", notes = "获取指定角色的权限列表")
    @ApiImplicitParam(name = "roleId", value = "角色ID", required = true, dataType = "Integer", paramType = "query")
    @GetMapping("/getByRoleId")
    @ResponseBody
    public Msg getPermissionsByRoleId(@RequestParam Integer roleId) {
        List<Permissions> permissions = permissionsService.selectByRoleId(roleId);
        return Msg.success().add("permissions", permissions);
    }
    
    /**
     * 添加新权限
     * @param permission 权限对象
     * @return 操作结果
     */
    @ApiOperation(value = "添加权限", notes = "添加新的权限")
    @RequiresPermissions("permissions:add")
    @PostMapping("/add")
    @ResponseBody
    public Msg add(@RequestBody Permissions permission) {
        boolean result = permissionsService.insert(permission);
        if (result) {
            return Msg.success().setMsg("添加权限成功");
        } else {
            return Msg.fail().setMsg("添加权限失败");
        }
    }
    
    /**
     * 更新权限
     * @param permission 权限对象
     * @return 操作结果
     */
    @ApiOperation(value = "更新权限", notes = "更新已有权限信息")
    @RequiresPermissions("permissions:update")
    @PostMapping("/update")
    @ResponseBody
    public Msg update(@RequestBody Permissions permission) {
        boolean result = permissionsService.update(permission);
        if (result) {
            return Msg.success().setMsg("更新权限成功");
        } else {
            return Msg.fail().setMsg("更新权限失败");
        }
    }
    
    /**
     * 删除权限
     * @param id 权限ID
     * @return 操作结果
     */
    @ApiOperation(value = "删除权限", notes = "根据ID删除权限")
    @ApiImplicitParam(name = "id", value = "权限ID", required = true, dataType = "Integer", paramType = "query")
    @RequiresPermissions("permissions:delete")
    @PostMapping("/delete")
    @ResponseBody
    public Msg delete(@RequestParam Integer id) {
        boolean result = permissionsService.delete(id);
        if (result) {
            return Msg.success().setMsg("删除权限成功");
        } else {
            return Msg.fail().setMsg("删除权限失败");
        }
    }

    /**
     * 获取所有权限的树形结构
     * @return 树形结构的权限列表
     */
    @ApiOperation(value = "获取权限树", notes = "获取所有权限的树形结构")
    @GetMapping("/listTree")
    @ResponseBody
    public Msg listTree() {
        List<Map<String, Object>> permissionsTree = permissionsService.buildPermissionsTree();
        return Msg.success().add("permissions", permissionsTree);
    }
} 